package com.changan.exception;

import com.changan.response.Result;
import com.changan.response.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler({ArithmeticException.class})
    public Result handlerArithmeticException(){
        return Result.error(ResultCode.ARITHMETIC_EXCEPTION);
    }

    @ExceptionHandler({NullPointerException.class})
    public Result handlerNullPointerException(Exception e){
        return Result.error(ResultCode.NULL_POINTER_EXCEPTION);
    }

    @ExceptionHandler({BusinessException.class})
    public Result handlerBusinessException(BusinessException e){
        log.error("出现了业务异常");
        return Result.error().code(e.getCode()).message(e.getErrMsg());
    }

    //....
    @ExceptionHandler({Exception.class})
    public Result handlerException(){
        //打印日志... 发送邮件或者短信通知管理员尽快处理bug
        log.error("出现了服务器异常....");
        return Result.error(ResultCode.UNKNOWN_EXCEPTION);
    }

}
